<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/view/include/taglib.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${project}/ds/css/input.css" rel="stylesheet" />
<link href="${project}/ds/lib/ligerUI/skins/Aqua/css/ligerui-all.css"
	rel="stylesheet" />
<link href="${project}/ds/lib/font-awesome-4.7.0/css/font-awesome.css"
	rel="stylesheet" />
<link href="${project}/ds/lib/ligerUI/skins/Gray2014/css/all.css"
	rel="stylesheet" />
<link href="${project}/ds/lib/layui2.4/css/layui.css" rel="stylesheet" />
<link href="${project}/ds/css/roomStyle.css" rel="stylesheet" />
<title>楼栋管理</title>
</head>
<body>
	<h1>楼栋管理</h1>
	<div>

		<div
			style="width: 110px; position: fixed; top: 0; left: 0; bottom: 0;">
			<div id="tree1"></div>
		</div>
		<div
			style="position: fixed; top: 0; left: 110px; bottom: 0; width: 100%;"
			class="layui-form">
			<div style="width: 60%; margin: 20px auto;" id="info">
				<fieldset class="layui-elem-field">
					<legend id="legendban">楼栋</legend>
					<div class="layui-field-box">
						<div class="layui-form-item">
							<label class="layui-form-label"></label>
							<div class="layui-btn-group">
								<div onclick="f_add()" class="layui-btn">新增</div>
								<!--<div onclick="f_update()" class="layui-btn">修改</div>-->
								<div class="layui-btn" onclick="del()">删除</div>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">楼栋</label>
							<div class="layui-input-block">
								<input type="hidden" id="D_ID" name="D_ID" /> 
								<input
									type="text" id="D_BanName" name="D_BanName" required
									lay-verify="required" placeholder="楼栋名称" autocomplete="off"
									class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">排序</label>
							<div class="layui-input-block">
								<input type="text" id="D_Order" name="D_Order"
									onkeyup="value = value.replace(/[^\d]/g, '')" placeholder="排序"
									autocomplete="off" class="layui-input"  >
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">认筹类型</label>
							<div class="layui-input-block">
								<!-- <input type="text" id="D_Type" name="D_Type"
									onkeyup="value = value.replace(/[^\d]/g, '')"
									placeholder="认筹类型" autocomplete="off" class="layui-input"> -->
								<select name="D_Type"  id="D_Type">
									<c:forEach var="item"   items="${renchous}">
										<option value="${item.money}">${item.money}</option>
									</c:forEach>						
								</select>
							</div>
						</div>
						<div class="layui-form-item">
							<div class="layui-input-block">
								<div class="layui-btn" lay-submit lay-filter="save">保存</div>
							</div>
						</div>
					</div>
				</fieldset>
			</div>
		</div>

		<div id="bandiv"
			style="width: 480px; display: none; margin-right: 20px" class="layui-form" lay-filter="layui-test">
			<div id="bantype"
				style="width: 100px; margin-top: 10px; margin-bottom: 10px; padding-left: 200px; font-size: 24px">
				添加楼栋</div>
			<div class="layui-form-item">
				<label class="layui-form-label">楼栋</label>
				<div class="layui-input-block">
					<input type="hidden" id="D_ID" name="D_ID" /> <input type="text"
						id="N_BanName" name="N_BanName" required lay-verify="required"
						placeholder="楼栋名称" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">排序</label>
				<div class="layui-input-block">
					<input type="text" id="N_Order" name="N_Order" 
						onkeyup="value = value.replace(/[^\d]/g, '')" placeholder="${maxOrder}"
						autocomplete="off" class="layui-input N_Order"  value="${maxOrder}">
				</div>
			</div>
			<!-- <div class="layui-form-item">
				<label class="layui-form-label">认筹类型</label>
				<div class="layui-input-block">
					<input type="text" id="N_Type" name="N_Type"
						onkeyup="value = value.replace(/[^\d]/g, '')" placeholder="认筹类型"
						autocomplete="off" class="layui-input">
				</div>
			</div> -->
			<div class="layui-form-item">
				<label class="layui-form-label">认筹类型</label>
				<div class="layui-input-block">
					<select name="N_Type" lay-filter="aihao" id="N_Type">
						<c:forEach var="item"   items="${renchous}">
							<option value="${item.money}">${item.money}</option>
						</c:forEach>						
					</select>
				</div>
			</div>
			<div style="width: 100px; margin-left: 218px;">
				<div class="layui-form-item">
					<div class="layui-btn" lay-submit lay-filter="add">确认</div>
				</div>
			</div>
		</div>
	</div>
	<script src="${project}/ds/lib/jquery/jquery-1.9.0.min.js"
		type="text/javascript"></script>
	<script src="${project}/ds/lib/ligerUI/js/core/base.js"
		type="text/javascript"></script>
	<script src="${project}/ds/lib/ligerUI/js/ligerui.min.js"
		type="text/javascript"></script>
	<script src="${project}/ds/lib/jquery.form.js" type="text/javascript"></script>
	<script src="${project}/ds/js/room/curd.js" type="text/javascript"></script>
	<script src="${project}/ds/js/room/command.js" type="text/javascript"></script>
	<script src="${project}/ds/lib/layui2.4/layui.js" type="text/javascript"></script>
	<script src="${project}/ds/js/js/ws.js" type="text/javascript"></script>
	<script src="${project}/ds/js/layeralert.js" type="text/javascript"></script>
	<script src="${project}/ds/js/XHD.js" type="text/javascript"></script>
	<script type="text/javascript">
		var tree;
		var layer;
		var layerwindow;
		var form;


		layui.use([ 'layer', 'form' ], function() {
			layer = layui.layer;
			form = layui.form;

			//ShowFormType();

			//保存事件
			form.on('submit(save)', function(data) {
				var field = data.field;
				field.Action = "saveban";
				if ($("#D_ID").val() == "") {
					layer.msg("请选择楼栋");
					return;
				}
				field.D_ID = $("#D_ID").val();
				field.D_BanName = $("#D_BanName").val();
				field.D_Order = $("#D_Order").val();
				field.D_Type = $("#D_Type").val();
				console.log(field);
				// CheckUrd(field, 0, "updateban");
				saveBan();
			});

		 	form.val("layui-test", {
				  "N_Order": "${maxOrder}"
				}) 
				//layer.msg($("#N_Order").val())
			//新增事件
			form.on('submit(add)', function(data) {			
				addsave();
			});
			
		});

		$(function() {
			tree = $("#tree1").ligerTree({
				url : '/loudong/getTree',
				//            idField: "D_ID",
				//            textFieldName: 'D_BanName',
				onSelect : onSelect,
				width : 130,
				checkbox : false,
				itemopen : false
			});
			// websocketConnect();
		});

		function addsave() {
			var b = true;
			$.ajax({
				url : "/loudong/checkExistLouDong",
				type : "POST",
				data : {
					T_BanName : $("#N_BanName").val()
				},
				async : false,
				success : function(result) {
					if (result === "false") {
						b = false;
					}
				}
			});
			if (b) {
				$.ajax({
					url : "/loudong/addLouDong",
					type : "post",
					data : {
						banName : $("#N_BanName").val(),
						order : $("#N_Order").val(),
						type : $("#N_Type").val()
					},
					dataType : "json",
					success : function(data) {
						console.log(data);
						var result = data.result;
						if (result === "true") {
							console.log("ok");

							f_reload();
							//reset();
							//closewait();
							layer.close(layerwindow);
							layer.msg("新增成功");
						}
					}
				});
			} else {
				layer.msg("该楼栋已存在");
			}
		}

		function saveBan() {
			// var field = window.roomdata;
			$.ajax({
				url : "/loudong/saveLouDong",
				type : "post",
				dataType : "json",
				data : "ID=" + $("#D_ID").val() + "&banName="
						+ $("#D_BanName").val() + "&order="
						+ $("#D_Order").val() + "&type=" + $("#D_Type").val(),
				success : function(data) {
					var result = data.result;
					layer.close(layerwindow);
					if (result === "true") {
						layer.msg("保存成功");
						f_reload();
						//wsLoadSql();
					} else {
						layer.msg(data.msg);
					}
				}
			});
		}

		/* function ShowFormType() {
			$
					.ajax({
						url : "../data/Dictionary.ashx",
						type : "post",
						dataType : "json",
						data : {
							Action : "getDetail",
							type : "apply",
							field : "formtype"
						},
						success : function(data) {
							data = data.Rows;
							for ( var i in data) {
								if (data.hasOwnProperty(i)) {
									var formtype = data[i].D_Text;
									var checkbox = $("<input type='radio' name='D_Type' value='" +
                            formtype +
                            "' " +
                            "title='" +
                            formtype +
                            "" +
                            "' />");
									$("#formtypediv").append(checkbox);
									form.render("radio");
								}
							}
						}
					});
		}
 */
		function del() {
			//CheckUrd('', 0, 'delete');
			ConfirmDeleteRoom();
		}

		//删除房间
		function ConfirmDeleteRoom() {
			if (tree.getSelected() === null) {
				layererr("请选择节点");
				return;
			}
			var node = tree.getSelected().data;
			console.log(node);
			var msg = "";
			var data = {
				rnd : Math.random(),
				banname : "0",
				id:0
			};
			if (node.children) {
				//删除所有
				msg = '该功能会删除项目所有房间，是否确认清除？';
			} else {
				//删除楼栋
				var banname = node.text;
				msg = '该功能会删除' + banname + '所有房间，是否确认清除？';
				data.banname = banname;
				data.id = node.D_ID;
			}
			layer.confirm(msg, {
				icon : 3,
				title : '提示'
			}, function(index) {
				layer.close(index);
				DeleteRoom(data);
			});
		}

		function DeleteRoom(data) {
			var load = layer.load(1, {
				time : 10000
			});

			//var HouseModel1 = GetRoomModel();
			$.ajax({
				url : "/loudong/deleteLouDong",
				type : "post",
				data : data,
				async : false,
				dataType : "json",
				success : function(data) {
					layer.close(load);
					if (data.result === "true") {
						//window.top.clearHouse();
						$("#name").val("");
						layer.msg("操作成功！");
						//wsLoadSql();
						f_reload();
						f_reset();
					} else {
						layer.msg(data.msg);
					}
				}
			});
			//var HouseModel2 = GetRoomModel();
			//var HouseModel3 = CheckRoomModel(HouseModel1, HouseModel2);
			//DeleteModel(HouseModel3);
		}

		/* function GetRoomModel() {
			var HouseModel;
			$.ajax({
				type : "GET",
				url : "../data/Tuser.ashx", // 注意后面的名字对应CS的方法名称 
				data : {
					Action : 'GetHouseModel'
				}, // 注意参数的格式和名称 
				contentType : "application/json; charset=utf-8",
				dataType : "json",
				async : false,
				success : function(result) {
					HouseModel = result;
				}
			});
			return HouseModel;
		} */

		/* function DeleteModel(Model) {
			for (var i = 0; i < Model.length; i++) {
				$.ajax({
					url : "http://122.228.248.78:19000/DeleteImage?Name="
							+ Model[i],
					type : "Get",
					async : false,
					success : function(d) {

					}
				});
			}
		} */

		/* function CheckRoomModel(Model1, Model2) {
			var len = Model1.length;
			var arr = [];

			while (len--) {
				if (Model2.indexOf(Model1[len]) < 0) {
					arr.push(Model1[len]);
				}
			}

			return arr;
		}
 */
		function onSelect(node) {
			$("input[name^=D_Type]").removeAttr("checked");
			var data = node.data;
			console.log(data);
			$("#D_ID").val(data.D_ID);
			$("#D_BanName").val(data.D_BanName);
			$("#D_Order").val(data.D_Order);
			//$("#D_Type").val(data.D_Type);
			$("select[name='D_Type']").val(data.D_Type);
		/* 	$("#N_ID").val(data.D_ID);
			$("#N_BanName").val(data.D_BanName);
			$("#N_Order").val(data.D_Order);
			$("#N_Type").val(data.D_Type);

			$("input[name^=D_Type][value=" + data.D_Type + "]").prop("checked","true");

			$("#legendban").html(data.D_BanName);
			form.render("radio"); */
			form.render("select");
		}

		function f_reload() {
			tree.reload();
		}

		function f_reset() {
			$("#legendban").html("楼栋");
			$("#D_ID").val("");
			$("#D_BanName").val("");
			$("#D_Order").val("");
			$("#D_Type").val("");
		}

		function f_add() {
			$("#N_ID").val("");
			$("#N_BanName").val("");
			//$("#N_Order").val("");
			$("#N_Type").val("");
			$("#bantype").html("添加楼栋");
			  $.ajax({
		            url: "/loudong/getLouDongMaxOrder",
		            type: "post",
		            async: true,
		            dataType: "json",
		            success: function(data) {
		            	$("#N_Order").val(data.msg);
		            	layerwindow = layer.open({
		    				type : 1,
		    				title : false,
		    				closeBtn : 0,
		    				area : [ '500px', '280px' ],
		    				skin : 'layui-layer-lan', //没有背景色
		    				shadeClose : true,
		    				content : $('#bandiv')
		    			});
		            }
		        });
			//layer.close();
			
		}

		function f_update() {
			if (tree.getSelected() === null) {
				layererr("请选择节点");
				return;
			}
			$("#bantype").html("修改楼栋");
			layer.close();
			layerwindow = layer.open({
				type : 1,
				title : false,
				closeBtn : 0,
				area : [ '500px', '280px' ],
				skin : 'layui-layer-lan', //没有背景色
				shadeClose : true,
				content : $('#bandiv')
			});
		}
	</script>
</body>
</html>